package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.AccountingdataModel;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateful;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Sawior
 */
@Stateful
public class AccountingdataDAOImpl implements AccountingdataDAO {
    
    private EntityManager entityManager;
    
    public AccountingdataDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public AccountingdataModel getAccountingDataById(Integer id) {
         return (AccountingdataModel) entityManager.createNamedQuery("AccountingdataModel.findByAccountingDataId").setParameter("accountingDataId", id).getSingleResult();
    }

    @Override
    public Collection<AccountingdataModel> getAccountingDataByName(String name) {
        return createCollectionFromList(entityManager.createNamedQuery("AccountingdataModel.findByName").setParameter("name", name).getResultList());
    }
    
    private Collection<AccountingdataModel> createCollectionFromList(List<Object> list) {
        Collection<AccountingdataModel> result = new ArrayList<AccountingdataModel>();
        for (Object listItem : list) {
            result.add((AccountingdataModel) listItem);
        }
        return result;
    }

    @Override
    public Collection<AccountingdataModel> getAccountingDataByCity(String city) {
        return createCollectionFromList(entityManager.createNamedQuery("AccountingdataModel.findByCity").setParameter("city", city).getResultList());
    }

    @Override
    public Collection<AccountingdataModel> getAccountingDataByZipCode(String zipCode) {
        return createCollectionFromList(entityManager.createNamedQuery("AccountingdataModel.findByZipNumber").setParameter("zipNumber", zipCode).getResultList());
    }

    @Override
    public Collection<AccountingdataModel> getAccountingDataByPayerType(String payerType) {
        return createCollectionFromList(entityManager.createNamedQuery("AccountingdataModel.findByPayerType").setParameter("payerType", payerType).getResultList());
    }

    @Override
    public void addAccountingData(AccountingdataModel accountingData) {
        entityManager.getTransaction().begin();
        entityManager.persist(accountingData);
        entityManager.getTransaction().commit();
    }

    @Override
    public void removeAccountingData(Integer id) {
        AccountingdataModel rowToDelete = getAccountingDataById(id);
        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();
    }

    @Override
    public AccountingdataModel updateAccountingData(AccountingdataModel accountingData) {
        entityManager.getTransaction().begin();
        AccountingdataModel actual = getAccountingDataById(accountingData.getAccountingDataId());
        entityManager.persist(actual);
        actual.setCity(accountingData.getCity());
        actual.setComments(accountingData.getComments());
        actual.setHouseNumber(accountingData.getHouseNumber());
        actual.setLocalNumber(accountingData.getLocalNumber());
        actual.setName(accountingData.getName());
        actual.setNipNumber(accountingData.getNipNumber());
        actual.setPhone(accountingData.getPhone());
        actual.setStreet(accountingData.getStreet());
        actual.setZipNumber(accountingData.getZipNumber());
        actual.setPayerType(accountingData.getPayerType());
        entityManager.getTransaction().commit();
        
        return getAccountingDataById(accountingData.getAccountingDataId());
    }

    @Override
    public int getAccountingDataSize() {
        return entityManager.createNamedQuery("AccountingdataModel.findAll").getResultList().size();
    }

    @Override
    public List<AccountingdataModel> getAllAccountingData() {
        return (List<AccountingdataModel>) entityManager.createNamedQuery("AccountingdataModel.findAll").getResultList();
    }

    @Override
    public void updateAccData(AccountingdataModel acc) {

        try{
            AccountingdataModel actual = getAccountingDataById(acc.getAccountingDataId());
            entityManager.getTransaction().begin();
            entityManager.persist(actual);
            actual.setCity(acc.getCity());
            actual.setComments(acc.getComments());
            actual.setHouseNumber(acc.getHouseNumber());
            actual.setLocalNumber(acc.getLocalNumber());
            actual.setName(acc.getName());
            actual.setNipNumber(acc.getNipNumber());
            actual.setPayerType(acc.getPayerType());
            actual.setPhone(acc.getPhone());
            actual.setStreet(acc.getStreet());
            actual.setZipNumber(acc.getZipNumber());
            entityManager.getTransaction().commit();
        }
        catch(Throwable t){
            updateAccData(acc);
        }
    }

}
